﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//Class that permits to write a log file but serve also as an event handler for the console textbox

namespace LeapInterface
{

    public class DataEventArgs : EventArgs
    {
        public string Property { get; internal set; }
        //public object OldValue { get; internal set; }
        //public object NewValue { get; internal set; }
        public DataEventArgs(string property)
        {
            this.Property = property;
            //this.OldValue = oldValue;
            //this.NewValue = newValue;
        }
    }

    public static class Logger
    {
        //public static StreamWriter file = File.AppendText("logLeap.txt");
        //private static FileStream fileStreamW = new FileStream("logleap.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
        //private static FileStream fileStreamR = new FileStream("logleap.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
        //private static TextWriter tw = new StreamWriter(fileStreamW);
        //private static TextReader tr = new StreamReader(fileStreamR);
        public static StreamWriter file = File.AppendText("logleap.txt");
        private static String logLast = "";
        private static　DateTime now;

        public delegate void DataEventHandler(object sender, DataEventArgs e);

        public static event DataEventHandler dataEvent;

        public static void Log(string message)
        {
            string s = "";
            logLast = message;
            now = DateTime.Now;
            s += now;
            s += " : ";
            s += message;
            file.WriteLine(s);
            file.Flush();
            //OnPropertyChange(this, new DataEventArgs("logAdded"));
            dataEvent(null, new DataEventArgs("Log Added"));
        }
        
        public static void OnPropertyChange(object sender, DataEventArgs e)
        {
            // Check if there are any Subscribers
            if (dataEvent != null && MyGlobals.gui)
            {
                // Call the Event
                dataEvent(null, e);
            }
        }
        
        public static string GetLastLog()
        {
            return logLast;
        }
    }
}
